package com.ruoyi.doc.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.doc.domain.ContentType;
import com.ruoyi.doc.service.IContentTypeService;
import org.apache.ofbiz.base.util.UtilGenerics;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.service.ServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 内容类型Controller
 *
 * @author yuanpingfang
 * @date 2021-03-30
 */
@RestController
@RequestMapping("/doc/contenttype")
public class ContentTypeController extends BaseController {
    @Autowired
    private IContentTypeService contentTypeService;

    /**
     * 查询内容类型列表
     */
    @PreAuthorize("@ss.hasPermi('doc:contenttype:list')")
    @GetMapping("/list")
    public TableDataInfo list(ContentType contentType) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String orderBy = "";
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        }
        Map<String, Object> returnMap = contentTypeService.selectContentTypeList(contentType, pageNum, pageSize, orderBy);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        if (!ServiceUtil.isSuccess(returnMap)) {
            rspData.setRows(Collections.emptyList());
            rspData.setTotal(0);
            return rspData;
        }
        List<ContentType> list = UtilGenerics.checkList(returnMap.get("rows"));
        Integer total = UtilMisc.toInteger(returnMap.get("total"));
        rspData.setRows(list);
        rspData.setTotal(total);
        return rspData;
    }

    /**
     * 导出内容类型列表
     */
    @PreAuthorize("@ss.hasPermi('doc:contenttype:export')")
    @Log(title = "内容类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ContentType contentType) {
        Map<String, Object> returnMap = contentTypeService.selectContentTypeList(contentType, 1, 999999, null);
        if (!ServiceUtil.isSuccess(returnMap)) { // 代表失败
            return AjaxResult.error((String) returnMap.get("errorMessage"));
        }
        List<ContentType> list = UtilGenerics.checkList(returnMap.get("rows"));
        ExcelUtil<ContentType> util = new ExcelUtil<ContentType>(ContentType.class);
        return util.exportExcel(list, "contenttype");
    }

    /**
     * 获取内容类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('doc:contenttype:query')")
    @GetMapping(value = "/{contentTypeId}")
    public AjaxResult getInfo(@PathVariable("contentTypeId") String contentTypeId) {
        Map<String, Object> returnMap = contentTypeService.selectContentTypeById(contentTypeId);
        if (!ServiceUtil.isSuccess(returnMap)) { // 代表失败
            return AjaxResult.error((String) returnMap.get("errorMessage"));
        }
        return AjaxResult.success(returnMap.get("contentType"));
    }

    /**
     * 新增内容类型
     */
    @PreAuthorize("@ss.hasPermi('doc:contenttype:add')")
    @Log(title = "内容类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ContentType contentType) {
        Map<String, Object> returnMap = contentTypeService.insertContentType(contentType);
        if (!ServiceUtil.isSuccess(returnMap)) { // 代表失败
            return AjaxResult.error((String) returnMap.get("errorMessage"));
        }
        return AjaxResult.success();
    }

    /**
     * 修改内容类型
     */
    @PreAuthorize("@ss.hasPermi('doc:contenttype:edit')")
    @Log(title = "内容类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ContentType contentType) {
        Map<String, Object> returnMap = contentTypeService.updateContentType(contentType);
        if (!ServiceUtil.isSuccess(returnMap)) { // 代表失败
            return AjaxResult.error((String) returnMap.get("errorMessage"));
        }
        return AjaxResult.success();
    }

    /**
     * 删除内容类型
     */
    @PreAuthorize("@ss.hasPermi('doc:contenttype:remove')")
    @Log(title = "内容类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{contentTypeIds}")
    public AjaxResult remove(@PathVariable String[] contentTypeIds) {
        Map<String, Object> returnMap = contentTypeService.deleteContentTypeByIds(contentTypeIds);
        if (!ServiceUtil.isSuccess(returnMap)) { // 代表失败
            return AjaxResult.error((String) returnMap.get("errorMessage"));
        }
        return AjaxResult.success();
    }
}
